package api;

import Util.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import problem.Problem;
import problem.ProblemDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import static problem.ProblemDAO.selectOne;

@WebServlet("/problem")//注解表示在 problem类触发以下代码
public class ProblemServlet extends HttpServlet {
    private Gson gson= new GsonBuilder().create();
    @Override
    //用来实现读取题目列表,指定题目详情
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置一下resp需要的属性
        resp.setStatus(200);//用来设定回应的状态码,服务器响应的状态码为200
        resp.setContentType("application/json; charset=utf-8");//使客户端浏览器，区分不同种类的数据
        //从req中读取 id这个参数
        String id = req.getParameter("id");
        if (id == null || id.equals("")){
            // 查找题目列表
            selectAll(resp);
        }else {
            selectOne(Integer.parseInt(id),resp);
            //查找指定题目详情
        }
    }

    private void selectAll(HttpServletResponse resp) throws IOException {
        //1.创建problem对象
        ProblemDAO problemDAO = new ProblemDAO();
        //2.查找所有结果
        List<Problem> problems = problemDAO.selectAll();
        //3.把结果包装成json模式
        String respString = gson.toJson(problems);
        //4.把结果写回前端
        resp.getWriter().write(respString);
    }
    private void selectOne(int problemId,HttpServletResponse resp) throws IOException {
        //1.创建problemDAO对象
        ProblemDAO problemDAO = new ProblemDAO();
        //2.查找指定结果
        Problem problem = problemDAO.selectOne(problemId);
        //3.把结果包装成json模式
        String respString = gson.toJson(problem);
        //4.把结果写回给客户端
        resp.getWriter().write(respString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //新增一个题目的数据
        //要读取到请求中的body数据,把读到的数据构造成problem对象
        //进一步就可以插入数据库了
        String body = HttpUtil.readBody(req);
        //把读到的数据转成problem对象
        Problem problem = gson.fromJson(body,Problem.class);
        //把数据插入到数据库
        ProblemDAO problemDAO = new ProblemDAO();
        problemDAO.insert(problem);
        //返回一个结果给客户端
        resp.setStatus(200);
        resp.setContentType("application/json; charset=utf-8");
        resp.getWriter().write("{\"ok\": 1}");
    }




    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除操作
        resp.setStatus(200);
        resp.setContentType("application/json; charset=utf-8");
        //1.从req请求对象中读取要删除的题目id
        String id = req.getParameter("id");
        if(id == null || id.equals("")){
            resp.getWriter().write("{\"ok\": 0,\"reason\": \"id 不存在\"}");
            return;
        }
        //2.调用数据库删除操作即可
        ProblemDAO problemDAO = new ProblemDAO();
        problemDAO.delete(Integer.parseInt(id));//要传入id的序号
        //返回删除结果
        resp.getWriter().write("{\"ok\": 1}");
    }
}
